package com.longge;

import java.util.ArrayList;

public class DistributeCandiesIIII {

    public long distributeCandies(int n, int limit) {

        int ans = 0;
        for (int i = 0; i <= limit; i++) {
            ArrayList<int[]> list = distributeCandiesTwo(n - i, limit);
            for (int[] res : list) {
                if (res[0] == res[1]) ans++;
                else ans += 2;
            }
        }
        return ans;
    }

    private ArrayList<int[]> distributeCandiesTwo(int twoCount, int limit) {
        ArrayList<int[]> res = new ArrayList<>();
        for (int i = 0; i <= twoCount / 2; i++) {
            int lastCount = twoCount - i;
            if (lastCount >= 0 && lastCount <= limit) {
                res.add(new int[]{i, lastCount});
            }
        }
        return res;
    }

    public static void main(String[] args) {
        long l = new DistributeCandiesIIII().distributeCandies(5, 2);
        System.out.println(l);
    }
}
